
<template>
  <el-card class="box-card col-center" shadow="never">
    <span class="box-card-title">自定义邮件内容</span>
    <div class="wangeditor-box">
      <Toolbar          style="border-bottom: 1px solid #ccc"
                        :editor="editorRef"
                        :defaultConfig="toolbarConfig"
                        :mode="mode"
      />
      <Editor
          class="editor-txt"
          v-model="valueHtml"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
      />
    </div>
    <el-form label-position="left" label-width="200px" style="margin-top: 20px">
      <el-form-item label="邮件主题">
        <el-input v-model="subject" placeholder="请输入邮件主题"></el-input>
      </el-form-item>
      <el-form-item label="自定义邮箱地址">
        <el-input v-model="email" placeholder="请输入邮箱地址"></el-input>
      </el-form-item>
      <el-form-item label="邮箱地址组">
        <el-checkbox-group v-model="selectedEmails">
          <el-checkbox
              v-for="item in emailOptions"
              :key="item.value"
              :label="item.value"
          >{{ item.label }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="sendEmail">发送邮件</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>import '@wangeditor/editor/dist/css/style.css' // 引入 css
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import axios from 'axios'

const editorRef = shallowRef()

// 内容 HTML
const valueHtml = ref('<p>hello</p>')

// 邮箱地址
const email = ref('')

const selectedEmails= ref([])

// 邮箱选项
const emailOptions = [
  { value: 'alliance', label: '联盟协作组' },
  { value: 'school', label: '试点院校' },
  { value: 'leader', label: '上级领导机构' }
]

// 邮箱主题
const subject = ref('')

// 模拟 ajax 异步获取内容
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = '<p>富文本编辑器</p>'
  }, 1500)
})

const toolbarConfig = {}
const editorConfig = { placeholder: '请输入内容...' }

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value
  if (editor == null) return
  editor.destroy()
})

const handleCreated = (editor) => {
  editorRef.value = editor // 记录 editor 实例，重要！
}

const sendEmail = () => {
  if (!email.value) {
    alert('请输入邮箱地址');
    return;
  }

  const data = {
    email: email.value,
    subject: subject.value,
    content: valueHtml.value,
    selectedEmails:selectedEmails.value
  };


  const API_URL = 'http://60.205.189.235:90/api/v1/email'

  axios.post(API_URL+"/sendSimpleMail", data)
      .then(response => {
        alert('邮件发送成功');
        valueHtml.value = '';
        email.value = '';
      })
      .catch(error => {
        console.error('Error sending email:', error);
        alert('邮件发送失败');
      });
}
</script>

<style lang="scss" scoped>.box-card {
  width: 100%;
  .wangeditor-box {
    border: 1px solid #f0f2f5;
    margin-top: 20px;
  }
  .editor-txt {
    height: 75vh !important;
    overflow-y: hidden;
    text-align: left;
  }
}
</style>